// pages/articledetail/articledetail.js
var api = require("../../config/api.js")
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    news: null,
    isShowCommentModal: false,
    reply: {},
    avatar:app.globalData.avatar
  },
  //发布评论-传入后端
  onClickPostComment: function () {
    console.log('评论内容:', this.data.reply)
    if (!this.data.reply.content) {
      wx.showToast({
        title: '评论不能为空',
        icon: 'error',
      })
    } else {
      console.log('this.data.reply:', this.data.reply)
      wx.request({
        url: api.CommentAPI,
        data: {
          news: this.data.reply.nid,
          depth: this.data.reply.depth,
          reply: this.data.reply.cid, //回复的id
          content: this.data.reply.content,
          root: this.data.reply.rid,
          token: app.globalData.token,
        },
        dataType: 'json',
        method: 'POST',
        responseType: 'text',
        timeout: 0,
        success: (result) => {
          if (result.data.code == 201){
            wx.showToast({
              title: result.data.msg,
              icon: 'error',
            })
          }
          if (result.statusCode == 201) {
            console.log(result.data);
            if (this.data.reply.rootindex == undefined) {
              // 如果是根评论, 添加到根评论  数组.push()   //后面添加 数组.unshift()//前面添加
              var dataList = this.data.news.comment;
              dataList.unshift(result.data)//前面添加
              this.setData({
                ['news.comment']: dataList,
                ['news.comment_count']: this.data.news.comment_count + 1
              });
            } else {//如果是子评论, 添加到子评论
              var childCommentList = this.data.news.comment[this.data.reply.rootindex].child;
              childCommentList.unshift(result.data)//前面添加
              this.setData({
                ['news.comment[' + this.data.reply.rootindex + '].child']: childCommentList,
                ['news.comment_count']: this.data.news.comment_count + 1
              });
            }

            this.onClickCancelCommentModal(); //关闭评论框
          }
        },
        fail: (res) => { },
        complete: (res) => { },
      })
    }
  },
  // 监听输入评论
  inputComment: function (e) {
    // console.log(e.detail.value)
    this.setData({
      ['reply.content']: e.detail.value
    })
  },
  // ×回复对象
  onClickClearReply: function () {
    this.setData({
      depth: 1,
      nid: this.data.reply.nid
      // ['reply.cid']:null,
      // ['reply.nickname']:null,
      // ['reply.depth']:1
    })
  },
  // 点写评论
  onClickShowCommentModal: function (e) {
    // 点回复
    console.log(e.currentTarget.dataset);

    this.setData({
      isShowCommentModal: true,
      reply: e.currentTarget.dataset
    })
  },
  onClickCancelCommentModal: function () {
    this.setData({
      isShowCommentModal: false,
      reply: {}
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var articleId = options.articleId
    wx.request({
      url: api.ArticleIdAPI + articleId + "/",
      data: {
        token: app.globalData.token,
        articleId: articleId
      },
      dataType: 'json',
      method: 'post',
      responseType: 'text',
      success: (result) => { },
    })

    console.log(options)
    console.log(app.globalData.token)
    wx.request({
      url: api.ArticleIdAPI + articleId + "/",
      data: '',
      dataType: 'json',
      method: 'GET',
      responseType: 'text',
      success: (result) => {
        console.log(result.data)
        this.setData({
          news: result.data
        })
      },
      fail: (res) => { },
      complete: (res) => { },
    })
  },
  // 显示更多评论
  getMore: function (e) {
    var root_id = e.currentTarget.dataset.root;
    var idx = e.currentTarget.dataset.idx;
    wx.request({
      url: api.CommentAPI,
      data: {
        root: root_id
      },
      method: "GET",
      dataType: 'json',
      responseType: 'text',
      success: (result) => {
        console.log(idx, result.data)
        // 从result.data 找到child, 把child替换成result.data内的
        this.setData({
          [["news.comment[" + idx + "].child"]]: result.data
        })
      },
      fail: (res) => { },
      complete: (res) => { },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  dianzan: function () {
    wx.request({
      url: api.UpAndDownAPI,
      data: {
        articleId: this.data.news.id,
        token: app.globalData.token,
        is_up: 1
      },
      dataType: "json",
      method: 'POST',
      responseType: 'text',
      success: (result) => {
        console.log(result.data)
        if (result.data.code == 200) {
          wx.showToast({
            title: '操作成功',
            icon: 'success',
          })
          this.data.up_num=this.data.up_num+1
        } else {
          wx.showToast({
            title: result.data.mas,
            icon: 'error',
          })
        }
      },
    })
  },
  diancai: function () {
    wx.request({
      url: api.UpAndDownAPI,
      data: {
        articleId: this.data.news.id,
        token: app.globalData.token,
        is_up: 0
      },
      dataType: "json",
      method: 'POST',
      responseType: 'text',
      success: (result) => {
        console.log(result.data)
        if (result.data.code == 200) {
          wx.showToast({
            title: '操作成功',
            icon: 'success',
          })
          this.data.down_num=this.data.down_num+1
        } else {
          wx.showToast({
            title: result.data.mas,
            icon: 'error',
          })
        }
      },
    })
  },
  onEditorReady() {
    wx.createSelectorQuery().select('#editor').context(res => {
      this.editorCtx = res.context;
      this.editorCtx.setContents({
        html: app.data.richTextContents,
        success: res => {
          console.log('[setContents success]')
        }
      })
    }).exec()
  }
})
